package com.zhu.rtda.heap.constant;

import com.zhu.rtda.heap.InstanceKlass;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//方法区
public class MethodArea {
    //类的成员变量指针 指向该类的一个实例
    private static final MethodArea METHOD_AREA_INSTANCE = new MethodArea();

    // 核心数据结构：类名 → Class 对象
    private final Map<String, InstanceKlass> classMap = new ConcurrentHashMap<>();

    public static MethodArea getInstance() {
        return METHOD_AREA_INSTANCE;
    }
    // 根据类名查找已加载的类
    public InstanceKlass findClass(String name) {
        return classMap.get(name);
    }

    // 注册新加载的类
    public void addClass(InstanceKlass clazz) {
        classMap.put(clazz.getName(), clazz);
    }

    // 判断类是否已加载
    public boolean containsClass(String name) {
        return classMap.containsKey(name);
    }

    public Map<String, InstanceKlass> getClassMap() {
        return classMap;
    }
}
